-- init variables
select @docCircAdminId:=DcUserId from DcUser where Login='DocCircAdmin';
select @docCircUserId:=DcUserId from DcUser where Login='DocCircUser';
select @usersRoleId:=DcRoleId from DcRole where RoleName='Users';
select @user1:=DcUserId from DcUser where Login='DocCircUser1';
select @user2:=DcUserId from DcUser where Login='DocCircUser2';
select @user3:=DcUserId from DcUser where Login='DocCircUser3';
select @docTypeFolderType:=FolderTypeId from FolderType where Name='DocType';
select @docTypeFolder:=FolderId from Folder where FolderType=@docTypeFolderType AND IsNull(ParentFolder);
select @testDocTypeFolder1:=FolderId from Folder where FolderType=@docTypeFolderType AND Name='Test DocType folder1';
select @notificationRIT:=RouteItemTypeId from RouteItemType where Name='Notification';
select @decisionRIT:=RouteItemTypeId from RouteItemType where Name='Decision';

-- Add DocType: "Test type first" into testDocTypeFolder1 by DocCircAdmin
insert into DocType (DocTypeId, Folder, Name, Description, Prefix, NextNumber, IsNew, DcUser) values 
(default, @testDocTypeFolder1, 'Test type first', 'first type', 'TTF', 1, false, @docCircAdminId); 
set @DocType1:=last_insert_id(); 
--
	insert into RoutePoint (DocType, OrderNum, Expression) Values
		(@DocType1, 1, '');
	set @lastRoutePoint:=last_insert_id();
	insert into RouteItem (RoutePoint, DocType, DcUser, RouteItemType, ItemIndex) Values 
		(@lastRoutePoint, @DocType1, @user1, @decisionRIT, 0);
	update RoutePoint set Expression='[0]' where RoutePointId=@lastRoutePoint;	
	
	insert into RoutePoint (DocType, OrderNum, Expression) Values
		(@DocType1, 2, '[0]');
	set @lastRoutePoint:=last_insert_id();
	insert into RouteItem (RoutePoint, DocType, DcUser, RouteItemType, ItemIndex) Values 
		(@lastRoutePoint, @DocType1, @user2, @decisionRIT, 0);
	
	insert into RoutePoint (DocType, OrderNum, Expression) Values
		(@DocType1, 3, '[0]');
	set @lastRoutePoint:=last_insert_id();
	insert into RouteItem (RoutePoint, DocType, DcUser, RouteItemType, ItemIndex) Values 
		(@lastRoutePoint, @DocType1, @user3, @decisionRIT, 0);
--
insert into DocTypePermission (DocType, DcRole, C, R, U, D, RO, UO, DO) 
	select @DocType1, DcRole, C, R, U, D, RO, UO, DO from NewDocTypePermission;
--

-- Add DocType: "Test type second" into Root DocType folder by DocCircUser2
insert into DocType (DocTypeId, Folder, Name, Description, Prefix, NextNumber, IsNew, DcUser) values 
(default, @docTypeFolder, 'Test type second', 'second type', 'TTS', 1, false, @user2);
set @DocType2:=last_insert_id(); 
--
	insert into RoutePoint (DocType, OrderNum, Expression) Values
		(@DocType2, 1, '[0]');
	set @lastRoutePoint:=last_insert_id();
	insert into RouteItem (RoutePoint, DocType, DcUser, RouteItemType, ItemIndex) Values 
		(@lastRoutePoint, @DocType2, @user3, @decisionRIT, 0);
	
	insert into RoutePoint (DocType, OrderNum, Expression) Values
		(@DocType2, 2, '[0]&&[1]');
	set @lastRoutePoint:=last_insert_id();
	--
		insert into RouteItem (RoutePoint, DocType, DcUser, RouteItemType, ItemIndex) Values 
			(@lastRoutePoint, @DocType2, @user1, @decisionRIT, 0);
		insert into RouteItem (RoutePoint, DocType, DcUser, RouteItemType, ItemIndex) Values 
			(@lastRoutePoint, @DocType2, @user2, @decisionRIT, 1);
	--
--
insert into DocTypePermission (DocType, DcRole, C, R, U, D, RO, UO, DO) 
	select @DocType2, DcRole, C, R, U, D, RO, UO, DO from NewDocTypePermission;
--
